package com.netsdk.demo.customize;

import com.netsdk.demo.util.CaseMenu;
import com.netsdk.lib.NetSDKLib;
import com.netsdk.lib.ToolKits;
import com.netsdk.lib.callback.impl.DefaultDisconnectCallback;
import com.netsdk.lib.callback.impl.DefaultHaveReconnectCallBack;
import com.netsdk.lib.structure.*;
import com.sun.jna.Memory;
import com.sun.jna.NativeLong;
import com.sun.jna.Pointer;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import static com.netsdk.lib.Utils.getOsPrefix;

/**
 * @author 291189
 * @version 1.0
 * @description ERR230904075 &ERR230904075-RC01
 * @date 2023/9/6 14:25
 */
public class LowSpaceDemo {

    // SDk对象初始化
    public static final NetSDKLib netsdk = NetSDKLib.NETSDK_INSTANCE;
    public static final NetSDKLib configsdk = NetSDKLib.CONFIG_INSTANCE;

    // 判断是否初始化
    private static boolean bInit = false;
    // 判断log是否打开
    private static boolean bLogOpen = false;
    // 设备信息
    private NetSDKLib.NET_DEVICEINFO_Ex deviceInfo = new NetSDKLib.NET_DEVICEINFO_Ex();
    // 登录句柄
    private NetSDKLib.LLong loginHandle = new NetSDKLib.LLong(0);

    // 回调函数需要是静态的，防止被系统回收
    // 断线回调
    private static NetSDKLib.fDisConnect disConnectCB = DefaultDisconnectCallback.getINSTANCE();
    // 重连回调
    private static NetSDKLib.fHaveReConnect haveReConnectCB = DefaultHaveReconnectCallBack.getINSTANCE();

    //订阅句柄
    NetSDKLib.LLong lAttachHandle = new NetSDKLib.LLong(0);
    // 编码格式
    public static String encode;

    static {
        String osPrefix = getOsPrefix();
        if (osPrefix.toLowerCase().startsWith("win32-amd64")) {
            encode = "GBK";
        } else if (osPrefix.toLowerCase().startsWith("linux-amd64")) {
            encode = "UTF-8";
        }
    }

    /**
     * 按照指定格式，获取当前时间
     */
    public static String getDate() {
        SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDate.format(new Date()).replaceAll("[^0-9]", "-");
    }

    /**
     * 初始化SDK库
     */
    public static boolean initSdk() {
        bInit = netsdk.CLIENT_Init(disConnectCB, null);// 进程启动时，初始化一次
        if (!bInit) {
            System.out.println("Initialize SDK failed");
            return false;
        }
        // 配置日志
        enableLog();
        // 设置断线重连回调接口, 此操作为可选操作，但建议用户进行设置
        netsdk.CLIENT_SetAutoReconnect(haveReConnectCB, null);
        // 设置登录超时时间和尝试次数，可选
        // 登录请求响应超时时间设置为3S
        int waitTime = 3000; //单位为ms
        // 登录时尝试建立链接 1 次
        int tryTimes = 1;
        // 设置连接设备超时时间和尝试次数
        netsdk.CLIENT_SetConnectTime(waitTime, tryTimes);
        // 设置更多网络参数， NET_PARAM 的nWaittime ， nConnectTryNum 成员与 CLIENT_SetConnectTime
        // 接口设置的登录设备超时时间和尝试次数意义相同,可选
        NetSDKLib.NET_PARAM netParam = new NetSDKLib.NET_PARAM();
        // 登录时尝试建立链接的超时时间
        netParam.nConnectTime = 10000;
        // 设置子连接的超时时间
        netParam.nGetConnInfoTime = 3000;
        //设置登陆网络环境
        netsdk.CLIENT_SetNetworkParam(netParam);
        return true;
    }

    /**
     * 打开 sdk log
     */
    private static void enableLog() {
        // SDK全局日志打印信息
        NetSDKLib.LOG_SET_PRINT_INFO setLog = new NetSDKLib.LOG_SET_PRINT_INFO();
        //设置日志路径
        File path = new File("sdklog/");
        //判断日志路径是否存在，若不存在则创建
        if (!path.exists()){
            path.mkdir();
        }
        // 这里的log保存地址依据实际情况自己调整
        String logPath = path.getAbsoluteFile().getParent() + "\\sdklog\\" + "sdklog" + getDate() + ".log";
        //日志输出策略,0:输出到文件(默认); 1:输出到窗口,
        setLog.nPrintStrategy = 0;
        //是否重设日志路径, 取值0否 ,1是
        setLog.bSetFilePath = 1;
        //日志路径(默认"./sdk_log/sdk_log.log")
        byte[] szLogFilePath = setLog.szLogFilePath;
        //自定义log保存地址，将数据logPath数据copy到LOG_SET_PRINT_INFO-->szLogFilePath变量中
        System.arraycopy(logPath.getBytes(), 0, szLogFilePath, 0, logPath.getBytes().length);
        //是否重设日志打印输出策略 取值0否 ,1是
        setLog.bSetPrintStrategy = 1;
        // 打开日志功能
        bLogOpen = netsdk.CLIENT_LogOpen(setLog);
        if (!bLogOpen){
            System.err.println("Failed to open NetSDK log "+ ToolKits.getErrorCode());
        }else {
            System.out.println("Success to open NetSDK log ");
        }
    }

    /**
     * 高安全登录
     */
    public void loginWithHighLevel() {
        // 输入结构体参数
        NetSDKLib.NET_IN_LOGIN_WITH_HIGHLEVEL_SECURITY pstlnParam = new NetSDKLib.NET_IN_LOGIN_WITH_HIGHLEVEL_SECURITY() {
            {
                // IP
                szIP = m_strIpAddr.getBytes();
                // 端口
                nPort = m_nPort;
                // 用户名
                szUserName = m_strUser.getBytes();
                // 密码
                szPassword = m_strPassword.getBytes();
            }
        };
        // 登录的输出结构体参数
        NetSDKLib.NET_OUT_LOGIN_WITH_HIGHLEVEL_SECURITY pstOutParam = new NetSDKLib.NET_OUT_LOGIN_WITH_HIGHLEVEL_SECURITY();
        // 高安全级别登陆
        loginHandle = netsdk.CLIENT_LoginWithHighLevelSecurity(pstlnParam, pstOutParam);
        if (loginHandle.longValue() == 0) {  //登陆失败
            System.err.printf("Login Device[%s] Port[%d]Failed. %s\n", m_strIpAddr, m_nPort, ToolKits.getErrorCode());

        } else {
            //登陆成功
            // 获取设备信息
            deviceInfo = pstOutParam.stuDeviceInfo;
            System.out.printf("Login Success Device Address[%s] 设备包含[%d]个通道 \n",m_strIpAddr,deviceInfo.byChanNum);
        }
    }

    /**
     * 退出
     */
    public void logOut() {
        //判断是否已登录
        if (loginHandle.longValue() != 0) {
            netsdk.CLIENT_Logout(loginHandle);
            System.out.println("LogOut Success");
        }
    }

    /**
     * 清理sdk环境并退出
     */
    public static void cleanAndExit() {
        //判断log是否打开
        if (bLogOpen) {
            // 关闭sdk日志打印
            netsdk.CLIENT_LogClose();
        }
        // 进程关闭时，调用一次
        netsdk.CLIENT_Cleanup();
        System.exit(0);
    }


    public static Pointer getStringToPointer(String src, String charset) {
        Pointer pointer = null;
        try {
            byte[] b = src.getBytes(charset);

            pointer = new Memory(b.length + 1);
            pointer.clear(b.length + 1);

            pointer.write(0, b, 0, b.length);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return pointer;
    }

    public static void StringToByteArr(String src, byte[] dst,String charName) {
        try {
            byte[] GBKBytes = src.getBytes(charName);
            for (int i = 0; i < GBKBytes.length; i++) {
                dst[i] = GBKBytes[i];
            }
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }

    /**
     * 订阅任务， start listen
     */
    public void AttachEventStartListen() {

        /// 订阅回调
        netsdk.CLIENT_SetDVRMessCallBack(MessCallBack.getInstance(), null);

        boolean bRet = netsdk.CLIENT_StartListenEx(loginHandle);

        if (bRet) {
            System.out.println("CLIENT_StartListenEx success.");
        } else {
            System.out.printf("CLIENT_StartListenEx fail, error:%s\n", ToolKits.getErrorCode());
        }
    }

    /**
     * 退订任务， stop listen
     */
    public void DetachEventStopListen() {
        boolean bRet = netsdk.CLIENT_StopListen(loginHandle);
        if (bRet) {
            System.out.println("CLIENT_StopListen success");
        } else {
            System.out.printf("CLIENT_StopListen fail, error:%s\n", ToolKits.getErrorCode());
        }
    }

    private static class MessCallBack implements NetSDKLib.fMessCallBack {

        private static MessCallBack instance;

        private MessCallBack() {
        }

        public static MessCallBack getInstance() {
            if (instance == null) {
                synchronized (MessCallBack.class) {
                    if (instance == null) {
                        instance = new MessCallBack();
                    }
                }
            }
            return instance;
        }

        @Override
		public boolean invoke(int lCommand, final NetSDKLib.LLong lLoginID,
                              Pointer pStuEvent, int dwBufLen, String strDeviceIP,
                              NativeLong nDevicePort, Pointer dwUser) {


            if (pStuEvent == null || dwBufLen <= 0) {
                return false;
            }

            /// 具体事件类,
            switch (lCommand) {
                case NetSDKLib.NET_ALARM_STORAGE_LOW_SPACE: // 存储容量不足事件(对应 ALARM_STORAGE_LOW_SPACE_INFO)
                {
                    ALARM_STORAGE_LOW_SPACE_INFO msg = new ALARM_STORAGE_LOW_SPACE_INFO();
                    ToolKits.GetPointerData(pStuEvent, msg);
                    System.out.println("存储容量不足事件");
                    System.out.println("nAction : " + msg.nAction);
                    try {
                        System.out.println("szName : " + new String(msg.szName,encode));

                        System.out.println("szDevice : " + new String(msg.szDevice,encode));

                        System.out.println("szGroup : " + new String(msg.szGroup,encode));
                        /**
                         总容量, byte
                         */
                        System.out.println("nTotalSpace : " + msg.nTotalSpace);
                        /**
                         剩余容量, byte
                         */
                        System.out.println("nFreeSpace : " + msg.nFreeSpace);
                        /**
                         已经使用的百分比
                         */
                        System.out.println("nPercent : " + msg.nPercent);

                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }

                    System.out.println("Time : " + msg.stuTime.toString());
                    break;
                }
                case NetSDKLib.NET_ALARM_GYROABNORMALATTITUDE: // 车辆的紧急制动、侧翻等状态导致的姿态异常进行报警(对应 ALARM_GYROABNORMALATTITUDE_INFO)
                {
                    ALARM_GYROABNORMALATTITUDE_INFO msg = new ALARM_GYROABNORMALATTITUDE_INFO();
                    ToolKits.GetPointerData(pStuEvent, msg);
                    System.out.println("车辆的紧急制动、侧翻等状态导致的姿态异常进行报警");
                    /**
                     事件动作,1表示持续性事件开始,2表示持续性事件结束;
                     */
                    System.out.println("nAction : " + msg.nAction);
                    /**
                     通道号
                     */
                    System.out.println("nChannelID : " + msg.nChannelID);
                    /**
                     事件发生时间
                     */
                    System.out.println("stuTime : " + msg.stuTime.toStringTime());
                    /**
                     事件ID
                     */
                    System.out.println("nEventID : " + msg.nEventID);
                    /**
                     当前翻滚角，单位：度
                     */
                    System.out.println("dbGyroCurRoll : " + msg.dbGyroCurRoll);

                    /**
                     当前俯仰角，单位：度
                     */
                    System.out.println("dbGyroCurPitch : " + msg.dbGyroCurPitch);

                    /**
                     当前加速度，单位：m/s^2
                     */
                    System.out.println("dbGyroCurAccel : " + msg.dbGyroCurAccel);

                    /**
                     事件发生时间
                     */
                    System.out.println("Time : " + msg.stuTime.toString());
                    break;
                }
                case NetSDKLib.NET_ALARM_IP_CONFLICT:  // IP冲突事件(对应结构体 ALARM_IP_CONFLICT_INFO)
                {
                    ALARM_IP_CONFLICT_INFO msg = new ALARM_IP_CONFLICT_INFO();
                    ToolKits.GetPointerData(pStuEvent, msg);
                    System.out.println("IP冲突事件");
                    /**
                     0:开始 1:停止
                     */
                    System.out.println("nAction : " + msg.nAction);
                    /**
                     事件触发时间
                     */
                    System.out.println("stuTime : " + msg.stuTime.toStringTime());

                    /**
                     IP冲突网卡名
                     */
                    try {
                        System.out.println("szInterface : " + new String(msg.szInterface,encode));
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                    /**
                     主机Mac地址个数
                     */
                    int nMacNum = msg.nMacNum;

                    /**
                     与Interface网卡IP地址冲突的主机Mac地址列表
                     */
                    byte[] szMac = msg.szMac;

                    for(int i=0;i<nMacNum;i++){
                        byte[]  mac=new byte[18];
                        System.arraycopy(szMac,i*mac.length,mac,0,mac.length);

                        try {
                            System.out.println("mac["+i+"]:" + new String(mac,encode));
                        } catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                }
                case NetSDKLib.NET_ALARM_LOGIN_FAILIUR:  // 登陆失败事件(对应结构体ALARM_LOGIN_FAILIUR_INFO)
                {
                    ALARM_LOGIN_FAILIUR_INFO msg = new ALARM_LOGIN_FAILIUR_INFO();
                    ToolKits.GetPointerData(pStuEvent, msg);
                    System.out.println("登陆失败事件");
                    /**
                     0:开始 1:停止
                     */
                    System.out.println("nAction:" + msg.nAction);

                    /**
                     * RealUTC 是否有效，bRealUTC 为 TRUE 时，用 stuRealUTC，否则 stuRealUTC 字段无效(用原事件结构体中的 事件发生时间/事件触发时间(UTC) 字段)
                     */
                    System.out.println("bRealUTC:" + msg.stuEventInfoEx.bRealUTC);
                    /**
                     * 事件发生的时间(标准UTC时间(不带时区夏令时偏差)), 由于事件的UTC时间在产品线之间使用的差异性, 故增加RealUTC作为标准UTC时间, 平台在收到事件解析首优先级是RealUTC, 其次是UTC.
                     */
                    System.out.println("stuTime:" + msg.stuEventInfoEx.stuRealUTC.toStringTime());

                    /**
                     序号
                     */
                    System.out.println("nSequence:" + msg.nSequence);

                    /**
                     事件名,填用户名称
                     */
                    try {
                        System.out.println("szName:" + new String(msg.szName,encode));
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }

                    /**
                     登录类型
                     */
                    try {
                        System.out.println("szType:" + new String(msg.szType,encode));
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }

                    /**
                     来源IP地址
                     */
                    try {
                        System.out.println("szAddr:" + new String(msg.szAddr,encode));
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                        /**
                         用户登陆失败错误码
                         */
                        System.out.println("nError:" + msg.nError);

                }
                default:
                    System.out.println("lCommand:"+lCommand);
                    break;
            }

            return true;

        }

    }


    int channel=0;
    public void AttachEventRealLoadPic() {

        this.DetachEventRealLoadPic();   // 先退订,部分设备不会对重复订阅作校验，重复订阅后会有重复的事件返回

        int bNeedPicture = 1;   // 需要图片

        // Demo里只模拟了一个通道的订阅所以把订阅句柄写成了全局, 如果订阅多个通道, 每个通道订阅后返回的句柄都不一样，请一一对应妥善保存
        lAttachHandle = netsdk.CLIENT_RealLoadPictureEx(loginHandle, channel, NetSDKLib.EVENT_IVS_ALL, bNeedPicture, AnalyzerDataCB.getInstance(), null, null);
        if (lAttachHandle.longValue() != 0) {
            System.out.printf("Channel [%d] CLIENT_RealLoadPictureEx Success\n", channel);
        } else {
            System.out.printf("Channel [%d] CLIENT_RealLoadPictureEx Failed!LastError = %s\n", channel, ToolKits.getErrorCode());
        }
    }

    /**
     * 报警事件（智能）回调
     */
    private static class AnalyzerDataCB implements NetSDKLib.fAnalyzerDataCallBack {
        private final File picturePath;
        private static AnalyzerDataCB instance;

        private AnalyzerDataCB() {
            picturePath = new File("./AnalyzerPicture/");
            if (!picturePath.exists()) {
                picturePath.mkdirs();
            }
        }

        public static AnalyzerDataCB getInstance() {
            if (instance == null) {
                synchronized (AnalyzerDataCB.class) {
                    if (instance == null) {
                        instance = new AnalyzerDataCB();
                    }
                }
            }
            return instance;
        }

        @Override
        public int invoke(NetSDKLib.LLong lAnalyzerHandle, int dwAlarmType, Pointer pAlarmInfo, Pointer pBuffer, int dwBufSize,
                          Pointer dwUser, int nSequence, Pointer reserved) {

            System.out.println("dwAlarmType--------------------"+ dwAlarmType);
            if (lAnalyzerHandle == null || lAnalyzerHandle.longValue() == 0) {
                return -1;
            }

            switch (dwAlarmType) {
                case NetSDKLib.EVENT_IVS_TRAFFIC_ROAD_CONSTRUCTION: {// 交通道路施工检测事件(对应 DEV_EVENT_TRAFFIC_ROAD_CONSTRUCTION_INFO)

                    System.out.println("交通道路施工检测事件" );

                    DEV_EVENT_TRAFFIC_ROAD_CONSTRUCTION_INFO msg = new DEV_EVENT_TRAFFIC_ROAD_CONSTRUCTION_INFO();
                    ToolKits.GetPointerData(pAlarmInfo, msg);

                    /**
                     * 事件动作, 0表示脉冲事件, 1表示持续性事件开始, 2表示持续性事件结束
                     */
                    System.out.println("nAction:"+msg.nAction);

                    /**
                     * 通道号
                     */
                    System.out.println("nChannelID:"+msg.nChannelID);

                    /**
                     * 事件名称
                     */
                    try {
                        System.out.println("szName:"+new String(msg.szName,encode));
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }

                    /**
                     * 事件发生的时间
                     */
                    System.out.println("UTC:"+msg.UTC.toStringTime());



                    int nImageInfoNum = msg.nImageInfoNum;
                    System.out.println("nImageInfoNum:"+nImageInfoNum);

                    NET_IMAGE_INFO_EX2[] stuImageInfo = new NET_IMAGE_INFO_EX2[msg.nImageInfoNum];
                    for(int i = 0; i < msg.nImageInfoNum; i ++){
                        stuImageInfo[i] = new NET_IMAGE_INFO_EX2();
                    }
                    ToolKits.GetPointerDataToStructArr(msg.pstuImageInfo, stuImageInfo);

                    for(int i=0;i<nImageInfoNum;i++){
                        NET_IMAGE_INFO_EX2 image= stuImageInfo[i];
                        if (image != null && image.nLength > 0) {
                            String picture = picturePath + "/" + System.currentTimeMillis() + "_ROAD_CONSTRUCTION.jpg";
                            ToolKits.savePicture(pBuffer, image.nOffset, image.nLength, picture);
                        }
                    }
                    if(nImageInfoNum==0){
                        String picture = picturePath + "/" + System.currentTimeMillis() + "_ROAD_CONSTRUCTION.jpg";

                        ToolKits.savePicture(pBuffer, 0, dwBufSize, picture);
                    }

                    break;
                }
                case NetSDKLib.EVENT_IVS_TRAFFIC_VEHICLE_BC : {// 飙车事件（对应 DEV_EVENT_TRAFFIC_VEHICLE_BC ）

                    System.out.println("飙车事件" );

                    DEV_EVENT_TRAFFIC_VEHICLE_BC msg = new DEV_EVENT_TRAFFIC_VEHICLE_BC();
                    ToolKits.GetPointerData(pAlarmInfo, msg);

                    /**
                     * 通道号
                     */
                    System.out.println("nChannelID:"+msg.nChannelID);
                    /**
                     事件发生的时间
                     */
                    System.out.println("UTC:"+msg.UTC.toStringTime());
                    /**
                     事件ID
                     */
                    System.out.println("nEventID:"+msg.nEventID);


                    int nImageInfoNum = msg.nImageInfoNum;

                    NET_IMAGE_INFO_EX2[] stuImageInfo = new NET_IMAGE_INFO_EX2[msg.nImageInfoNum];
                    for(int i = 0; i < msg.nImageInfoNum; i ++){
                        stuImageInfo[i] = new NET_IMAGE_INFO_EX2();
                    }
                    ToolKits.GetPointerDataToStructArr(msg.pstuImageInfo, stuImageInfo);


                    for(int i=0;i<nImageInfoNum;i++){
                        NET_IMAGE_INFO_EX2 image= stuImageInfo[i];
                        if (image != null && image.nLength > 0) {
                            String picture = picturePath + "/" + System.currentTimeMillis() + "_VEHICLE_BC.jpg";
                            ToolKits.savePicture(pBuffer, image.nOffset, image.nLength, picture);
                        }
                    }

                    if(nImageInfoNum==0){
                        String picture = picturePath + "/" + System.currentTimeMillis() + "_VEHICLE_BC.jpg";

                        ToolKits.savePicture(pBuffer, 0, dwBufSize, picture);
                    }
                        break;

                }
                case NetSDKLib.EVENT_IVS_VEHICLE_DISTANCE_NEAR : {// 安全驾驶车距过近报警事件(对应 DEV_EVENT_VEHICLE_DISTANCE_NEAR_INFO)

                    System.out.println("安全驾驶车距过近报警事件" );

                    DEV_EVENT_VEHICLE_DISTANCE_NEAR_INFO msg = new DEV_EVENT_VEHICLE_DISTANCE_NEAR_INFO();
                    ToolKits.GetPointerData(pAlarmInfo, msg);

                    /**
                     * 事件动作, 0表示脉冲事件, 1表示持续性事件开始, 2表示持续性事件结束
                     */
                    System.out.println("nAction:"+msg.nAction);

                    /**
                     * 事件发生的时间
                     */
                    System.out.println("UTC:"+msg.UTC.toStringTime());

                    /**
                     司机ID
                     */
                    try {
                        System.out.println("szDriverID:"+new String(msg.szDriverID,encode));
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }

                    /**
                     违章关联视频FTP上传路径
                     */
                    try {
                        System.out.println("szVideoPath:"+new String(msg.szVideoPath,encode));
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }

                    /**
                     违章关联的多个视频信息个数
                     */
                    int nRelatingVideoInfoNum = msg.nRelatingVideoInfoNum;

                    /**
                     违章关联的多个视频信息数组，最多支持16个视频
                     */
                       NET_RELATING_VIDEO_INFO[]					stuRelatingVideoInfo=msg.stuRelatingVideoInfo;

                    for(int i=0;i<nRelatingVideoInfoNum;i++){
                        NET_RELATING_VIDEO_INFO videoInfo = stuRelatingVideoInfo[i];
                        /**
                         * 违章关联视频FTP上传路径
                         */
                        byte[] szVideoPath = videoInfo.szVideoPath;
                        try {
                            System.out.println("szVideoPath["+i+"]:"+new String(szVideoPath,encode));
                        } catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }

                    }

                    break;

                }
                case NetSDKLib.EVENT_IVS_TRAFFIC_QUEUEJUMP : {  // 车辆加塞事件(对应 DEV_EVENT_TRAFFIC_QUEUEJUMP_INFO)

                    System.out.println("车辆加塞事件" );

                    DEV_EVENT_TRAFFIC_QUEUEJUMP_INFO msg = new DEV_EVENT_TRAFFIC_QUEUEJUMP_INFO();
                    ToolKits.GetPointerData(pAlarmInfo, msg);

                    /**
                     通道号
                     */
                    System.out.println("nChannelID:"+msg.nChannelID);

                    /**
                     * 事件发生的时间
                     */
                    System.out.println("UTC:"+msg.UTC.toStringTime());

                    /**
                     车辆信息
                     */
                     DEV_EVENT_TRAFFIC_TRAFFICCAR_INFO stTrafficCar=msg.stTrafficCar;
                    /**
                     * 速度    单位Km/H
                     */
                    int nSpeed = stTrafficCar.nSpeed;

                    System.out.println("nSpeed:"+nSpeed);

                    /**
                     * 车牌号码
                     */
                    byte[] szPlateNumber = stTrafficCar.szPlateNumber;

                    try {
                        System.out.println("szPlateNumber:"+new String(szPlateNumber,encode));

                        /**
                         * 号牌类型 "Unknown" 未知; "Normal" 蓝牌黑牌; "Yellow" 黄牌; "DoubleYellow" 双层黄尾牌
                         * "Police" 警牌;"SAR" 港澳特区号牌; "Trainning" 教练车号牌; "Personal" 个性号牌; "Agri" 农用牌
                         * "Embassy" 使馆号牌; "Moto" 摩托车号牌; "Tractor" 拖拉机号牌; "Other" 其他号牌
                         */
                        System.out.println("szPlateType:"+new String(stTrafficCar.szPlateType,encode));

                        /**
                         * 违章描述
                         */
                        System.out.println("szViolationDesc:"+new String(stTrafficCar.szViolationDesc,encode));


                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                    break;


                }
                case NetSDKLib.EVENT_IVS_TRAFFIC_SERPENTINE_CHANGE_LANE : {   // 蛇形变道事件(对应 DEV_EVENT_TRAFFIC_SERPENTINE_CHANGE_LANE_INFO)

                    System.out.println("蛇形变道事件" );

                    DEV_EVENT_TRAFFIC_SERPENTINE_CHANGE_LANE_INFO msg = new DEV_EVENT_TRAFFIC_SERPENTINE_CHANGE_LANE_INFO();
                    ToolKits.GetPointerData(pAlarmInfo, msg);

                    /**
                     通道号
                     */
                    System.out.println("nChannelID:"+msg.nChannelID);

                    /**
                     事件动作 0:脉冲
                     */
                    System.out.println("nAction:"+msg.nAction);


                    /**
                     事件发生的时间
                     */
                    System.out.println("stuUTC:"+msg.stuUTC.toStringTime());

                    /**
                     事件名称
                     */
                    byte[] szName
                            = msg.szName;
                    try {
                        System.out.println("szName:"+new String(szName,encode));
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }

                    /**
                     车辆信息
                     */
                    DEV_EVENT_TRAFFIC_TRAFFICCAR_INFO stTrafficCar=msg.stuTrafficCar;
                    /**
                     * 速度    单位Km/H
                     */
                    int nSpeed = stTrafficCar.nSpeed;

                    System.out.println("nSpeed:"+nSpeed);

                    /**
                     * 车牌号码
                     */
                    byte[] szPlateNumber = stTrafficCar.szPlateNumber;

                    try {
                        System.out.println("szPlateNumber:"+new String(szPlateNumber,encode));

                        /**
                         * 号牌类型 "Unknown" 未知; "Normal" 蓝牌黑牌; "Yellow" 黄牌; "DoubleYellow" 双层黄尾牌
                         * "Police" 警牌;"SAR" 港澳特区号牌; "Trainning" 教练车号牌; "Personal" 个性号牌; "Agri" 农用牌
                         * "Embassy" 使馆号牌; "Moto" 摩托车号牌; "Tractor" 拖拉机号牌; "Other" 其他号牌
                         */
                        System.out.println("szPlateType:"+new String(stTrafficCar.szPlateType,encode));

                        /**
                         * 违章描述
                         */
                        System.out.println("szViolationDesc:"+new String(stTrafficCar.szViolationDesc,encode));


                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }

                    break;

                }
                case NetSDKLib.EVENT_IVS_TRAFFIC_CHANGE_LANE_CONTINUES : { //机动车连续变道违法事件(对应 DEV_EVENT_TRAFFIC_CHANGE_LANE_CONTINUES_INFO)

                    System.out.println("机动车连续变道违法事件" );

                    DEV_EVENT_TRAFFIC_CHANGE_LANE_CONTINUES_INFO msg = new DEV_EVENT_TRAFFIC_CHANGE_LANE_CONTINUES_INFO();
                    ToolKits.GetPointerData(pAlarmInfo, msg);

                    /**
                     事件名称
                     */
                    byte[] szName
                            = msg.szName;
                    try {
                        System.out.println("szName:"+new String(szName,encode));
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }

                    /**
                     通道号
                     */
                    System.out.println("nChannelID:"+msg.nChannelID);

                    /**
                     事件动作 0:脉冲
                     */
                    System.out.println("nAction:"+msg.nAction);


                    /**
                     事件发生的时间
                     */
                    System.out.println("stuUTC:"+msg.stuUTC.toStringTime());
                        /**
                         车速，单位km/h
                         */

                    int nSpeed1 = msg.nSpeed;
                    System.out.println("nSpeed1:"+nSpeed1);

                    /**
                     车辆信息
                     */
                    DEV_EVENT_TRAFFIC_TRAFFICCAR_INFO stTrafficCar=msg.stuTrafficCar;
                    /**
                     * 速度    单位Km/H
                     */
                    int nSpeed = stTrafficCar.nSpeed;

                    System.out.println("nSpeed:"+nSpeed);

                    /**
                     * 车牌号码
                     */
                    byte[] szPlateNumber = stTrafficCar.szPlateNumber;

                    try {
                        System.out.println("szPlateNumber:"+new String(szPlateNumber,encode));

                        /**
                         * 号牌类型 "Unknown" 未知; "Normal" 蓝牌黑牌; "Yellow" 黄牌; "DoubleYellow" 双层黄尾牌
                         * "Police" 警牌;"SAR" 港澳特区号牌; "Trainning" 教练车号牌; "Personal" 个性号牌; "Agri" 农用牌
                         * "Embassy" 使馆号牌; "Moto" 摩托车号牌; "Tractor" 拖拉机号牌; "Other" 其他号牌
                         */
                        System.out.println("szPlateType:"+new String(stTrafficCar.szPlateType,encode));

                        /**
                         * 违章描述
                         */
                        System.out.println("szViolationDesc:"+new String(stTrafficCar.szViolationDesc,encode));


                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                            break;
                }
                case NetSDKLib.EVENT_IVS_TRAFFIC_OVERTAKE_ONRIGHT : { // 右侧超车事件(对应 DEV_EVENT_TRAFFIC_OVERTAKE_ONRIGHT_INFO)

                    System.out.println("右侧超车事件" );

                    DEV_EVENT_TRAFFIC_OVERTAKE_ONRIGHT_INFO msg = new DEV_EVENT_TRAFFIC_OVERTAKE_ONRIGHT_INFO();
                    ToolKits.GetPointerData(pAlarmInfo, msg);

                    /**
                     通道号
                     */
                    System.out.println("nChannelID:"+msg.nChannelID);

                    /**
                     事件动作 0:脉冲
                     */
                    System.out.println("nAction:"+msg.nAction);


                    /**
                     事件发生的时间
                     */
                    System.out.println("stuUTC:"+msg.stuUTC.toStringTime());


                    /**
                     车辆信息
                     */
                    DEV_EVENT_TRAFFIC_TRAFFICCAR_INFO stTrafficCar=msg.stuTrafficCar;
                    /**
                     * 速度    单位Km/H
                     */
                    int nSpeed = stTrafficCar.nSpeed;

                    System.out.println("nSpeed:"+nSpeed);

                    /**
                     * 车牌号码
                     */
                    byte[] szPlateNumber = stTrafficCar.szPlateNumber;

                    try {
                        System.out.println("szPlateNumber:"+new String(szPlateNumber,encode));

                        /**
                         * 号牌类型 "Unknown" 未知; "Normal" 蓝牌黑牌; "Yellow" 黄牌; "DoubleYellow" 双层黄尾牌
                         * "Police" 警牌;"SAR" 港澳特区号牌; "Trainning" 教练车号牌; "Personal" 个性号牌; "Agri" 农用牌
                         * "Embassy" 使馆号牌; "Moto" 摩托车号牌; "Tractor" 拖拉机号牌; "Other" 其他号牌
                         */
                        System.out.println("szPlateType:"+new String(stTrafficCar.szPlateType,encode));

                        /**
                         * 违章描述
                         */
                        System.out.println("szViolationDesc:"+new String(stTrafficCar.szViolationDesc,encode));


                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                    break;

                }
                case NetSDKLib.EVENT_IVS_TRAFFIC_TRUCK_OCCUPIED : { // 大车占道事件(对应 DEV_EVENT_TRAFFIC_TRUCK_OCCUPIED_INFO)

                    System.out.println("大车占道事件" );

                    DEV_EVENT_TRAFFIC_TRUCK_OCCUPIED_INFO msg = new DEV_EVENT_TRAFFIC_TRUCK_OCCUPIED_INFO();
                    ToolKits.GetPointerData(pAlarmInfo, msg);

                    /**
                     通道号
                     */
                    System.out.println("nChannelID:"+msg.nChannelID);

                    /**
                     事件动作 0:脉冲
                     */
                    System.out.println("nAction:"+msg.nAction);


                    /**
                     事件发生的时间
                     */
                    System.out.println("stuUTC:"+msg.stuUTC.toStringTime());


                    /**
                     车辆信息
                     */
                    DEV_EVENT_TRAFFIC_TRAFFICCAR_INFO stTrafficCar=msg.stuTrafficCar;
                    /**
                     * 速度    单位Km/H
                     */
                    int nSpeed = stTrafficCar.nSpeed;

                    System.out.println("nSpeed:"+nSpeed);
                    /**
                     对应车道号
                     */
                    System.out.println("nLane:"+msg.nLane);

                    /**
                     * 车牌号码
                     */
                    byte[] szPlateNumber = stTrafficCar.szPlateNumber;

                    try {
                        System.out.println("szPlateNumber:"+new String(szPlateNumber,encode));

                        /**
                         * 号牌类型 "Unknown" 未知; "Normal" 蓝牌黑牌; "Yellow" 黄牌; "DoubleYellow" 双层黄尾牌
                         * "Police" 警牌;"SAR" 港澳特区号牌; "Trainning" 教练车号牌; "Personal" 个性号牌; "Agri" 农用牌
                         * "Embassy" 使馆号牌; "Moto" 摩托车号牌; "Tractor" 拖拉机号牌; "Other" 其他号牌
                         */
                        System.out.println("szPlateType:"+new String(stTrafficCar.szPlateType,encode));

                        /**
                         * 违章描述
                         */
                        System.out.println("szViolationDesc:"+new String(stTrafficCar.szViolationDesc,encode));


                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                    break;

                }   case NetSDKLib.EVENT_IVS_TRAFFICACCIDENT : { //交通事故事件(对应 DEV_EVENT_TRAFFICACCIDENT_INFO)

                    System.out.println("交通事故事件" );

                    DEV_EVENT_TRAFFICACCIDENT_INFO msg = new DEV_EVENT_TRAFFICACCIDENT_INFO();
                    ToolKits.GetPointerData(pAlarmInfo, msg);

                    /**
                     通道号
                     */
                    System.out.println("nChannelID:"+msg.nChannelID);

                    /**
                     事件名称
                     */
                    try {
                        System.out.println("szName:"+ new String(msg.szName,encode) );
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }

                    /**
                     事件发生的时间
                     */
                    System.out.println("UTC:"+msg.UTC.toStringTime());

                    int nEventID = msg.nEventID;
                    /**
                     事件ID
                     */
                    System.out.println("nEventID:"+nEventID);
                    /**
                     * 图片信息个数
                     */
                    int nImageInfoNum = msg.nImageInfoNum;
                    /**
                     * 图片信息数组 {@link com.netsdk.lib.structure.NET_IMAGE_INFO_EX3}
                     */
                    Pointer pstuImageInfo = msg.pstuImageInfo;

                    NET_IMAGE_INFO_EX3[]   images= new NET_IMAGE_INFO_EX3[nImageInfoNum];
                    for(int i=0;i<images.length;i++){
                        images[i]=new NET_IMAGE_INFO_EX3();
                    }


                    ToolKits.GetPointerDataToStructArr(pstuImageInfo,images);

                    for(int i=0;i<images.length;i++){
                        NET_IMAGE_INFO_EX3 image = images[i];
                        if (image != null && image.nLength > 0) {
                            String picture = picturePath + "/" + System.currentTimeMillis() + "_TRAFFICACCIDENT.jpg";
                            ToolKits.savePicture(pBuffer, image.nOffset, image.nLength, picture);
                        }
                    }



                    if(nImageInfoNum==0){
                        String picture = picturePath + "/" + System.currentTimeMillis() + "_TRAFFICACCIDENT.jpg";

                        ToolKits.savePicture(pBuffer, 0, dwBufSize, picture);
                    }

                    break;

                }
                case NetSDKLib.EVENT_IVS_FIREDETECTION : { //火警事件(对应 DEV_EVENT_FIRE_INFO)

                    System.out.println("火警事件" );

                    DEV_EVENT_FIRE_INFO msg = new DEV_EVENT_FIRE_INFO();
                    ToolKits.GetPointerData(pAlarmInfo, msg);

                    /**
                     通道号
                     */
                    System.out.println("nChannelID:"+msg.nChannelID);

                    /**
                     事件名称
                     */
                    try {
                        System.out.println("szName:"+ new String(msg.szName,encode) );
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }

                    /**
                     事件发生的时间
                     */
                    System.out.println("UTC:"+msg.UTC.toStringTime());

                    int nEventID = msg.nEventID;
                    /**
                     事件ID
                     */
                    System.out.println("nEventID:"+nEventID);

                    /**
                     * 事件动作,0表示脉冲事件,1表示持续性事件开始,2表示持续性事件结束
                     * */
                    byte bEventAction = msg.bEventAction;
                    System.out.println("bEventAction:"+bEventAction);

                    break;

                }
                case NetSDKLib.EVENT_IVS_SMOKEDETECTION : { // 烟雾报警事件(对应 DEV_EVENT_SMOKE_INFO)

                    System.out.println("烟雾报警事件" );

                    DEV_EVENT_SMOKE_INFO msg = new DEV_EVENT_SMOKE_INFO();
                    ToolKits.GetPointerData(pAlarmInfo, msg);

                    /**
                     通道号
                     */
                    System.out.println("nChannelID:"+msg.nChannelID);

                    /**
                     事件名称
                     */
                    try {
                        System.out.println("szName:"+ new String(msg.szName,encode) );
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }

                    /**
                     事件发生的时间
                     */
                    System.out.println("UTC:"+msg.UTC.toStringTime());

                    int nEventID = msg.nEventID;
                    /**
                     事件ID
                     */
                    System.out.println("nEventID:"+nEventID);

                    /**
                     * 事件动作,0表示脉冲事件,1表示持续性事件开始,2表示持续性事件结束
                     * */
                    byte bEventAction = msg.bEventAction;
                    System.out.println("bEventAction:"+bEventAction);

                    break;

                }
                default:
                    System.out.println("其他事件--------------------"+ dwAlarmType);
                    break;
            }
            return 0;
        }
    }

    /**
     * 停止侦听智能事件
     */
    public void DetachEventRealLoadPic() {
        if (lAttachHandle.longValue() != 0) {
            netsdk.CLIENT_StopLoadPic(lAttachHandle);
        }
    }

    public void runTest()
    {
        System.out.println("Run Test");
        CaseMenu menu = new CaseMenu();;
        menu.addItem(new CaseMenu.Item(this, "订阅不带图事件", "AttachEventStartListen"));
        menu.addItem(new CaseMenu.Item(this, "停止订阅不带图事件", "DetachEventStopListen"));
        menu.addItem(new CaseMenu.Item(this, "订阅智能事件", "AttachEventRealLoadPic"));
        menu.addItem(new CaseMenu.Item(this, "停止侦听智能事件", "DetachEventRealLoadPic"));
        menu.run();
    }

    // 配置登陆地址，端口，用户名，密码
    private String m_strIpAddr = "172.13.76.5";
    private int m_nPort = 37777;
    private String m_strUser = "mao_xiangying";
    private String m_strPassword = "admin123";
    public static void main(String[] args) {
        LowSpaceDemo demo=new LowSpaceDemo();
        demo.initTest();
        demo.runTest();
        demo.endTest();
    }

    /**
     * 初始化测试
     */
    public void initTest() {
        initSdk();

        this.loginWithHighLevel();
    }

    /**
     * 结束测试
     */
    public void endTest() {
        System.out.println("End Test");
        this.logOut(); // 登出设备
        System.out.println("See You...");
        cleanAndExit(); // 清理资源并退出
    }

}
